<template>
    <view>
        <view v-if="(company || null) != null">
            <form @submit="form_submit" class="form-container">
                <view class="page-bottom-fixed padding-main">
					<view class="padding-main">
						<view class="cr-red">您可以在此维护一些信息</view>
					</view> 
					<view class="bg-white border-radius-main pr oh spacing-mb">
					    <view class="form-gorup">
					        <view class="form-gorup-title padding-right-main fw-b"><text class="cr-red text-size-xs margin-right-sm">*</text>公司简介</view>
							<textarea placeholder="公司简介长度不超过10000" name="com_describe" placeholder-class="cr-grey-9" class="cr-base" maxlength="100000" :value="company.com_describe || ''"></textarea>
					    </view>
					</view>
					<view class="bg-white border-radius-main pr oh spacing-mb spacing-mt">
					    <view class="form-gorup">
					        <view class="form-gorup-title padding-right-main fw-b"><text class="cr-red text-size-xs margin-right-sm">*</text>公司客服二维码</view>
							<view class="margin-top-main">
								<component-upload :propData="image_list" :propMaxNum="1" propDelServ="yes" :propDelServUrl="del_serv_url" :propPathType="editor_path_type" @call-back="retrun_image_event"></component-upload>
							</view>
					    </view>
					</view>
					<view class="bg-white border-radius-main pr oh spacing-mb">
					    <view class="form-gorup">
					        <view class="flex-row jc-sb align-c">
					            <view class="form-gorup-title padding-right-main"><text class="cr-red text-size-xs margin-right-sm">*</text>联系微信号</view>
								<input type="text" placeholder-class="cr-grey-9" placeholder="输入公司联系微信号" name="contact_wx" class="cr-base flex-1 flex-width tr" :value="company.contact_wx || ''" maxlength="100" />
					        </view>
					    </view>
					</view>
					<view class="bg-white border-radius-main pr oh spacing-mb">
					    <view class="form-gorup">
					        <view class="flex-row jc-sb align-c">
					            <view class="form-gorup-title padding-right-main"><text class="cr-red text-size-xs margin-right-sm">*</text>联系电话</view>
								<input type="text" placeholder-class="cr-grey-9" placeholder="输入公司联系电话" name="contact_tel" class="cr-base flex-1 flex-width tr" :value="company.contact_tel || ''" maxlength="20" />
					        </view>
					    </view>
					</view>

					<view class="bg-white border-radius-main pr oh">
					    <view class="form-gorup">
					        <view class="form-gorup-title padding-right-main"><text class="cr-red text-size-xs margin-right-sm">*</text>{{$t('user-detail.user-detail.7cufw6')}}</view>
					        <view class="margin-top-main sp-editor">
					            <!-- <sp-editor :templates="(data || null) !== null ? data.content : ''" @input="rich_text_event" @upinImage="up_in_image_event"></sp-editor> -->
					            <sp-editor @init="initEditor" @input="rich_text_event" @upinImage="up_in_image_event"></sp-editor>
					        </view>
					    </view>
					</view>
                </view>
                <!-- 底部操作 -->
                <view class="bottom-fixed btn-bottom bg-white">
                    <view class="oh bottom-line-exclude">
                        <button class="cr-white round text-size wh-auto" type="default" form-type="submit" hover-class="none" :loading="form_submit_loading" :disabled="form_submit_loading" :style="'background:linear-gradient(107deg, ' + diytheme.main_color + ' 0%, ' + diytheme.main_color + ' 100%)'">{{$t('common.submit')}}</button>
                    </view>
                </view>
            </form>
        </view>
        <view v-else>
            <!-- 提示信息 -->
            <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
        </view>

		<!-- #ifdef APP-PLUS -->
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup>
		<!-- #endif -->
    </view>
</template>
<script>
    const app = getApp(); 
	// #ifdef APP-PLUS
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	// #endif
    import componentPopup from '@/components/popup/popup';
    import componentUpload from '@/components/upload/upload';
    import componentNoData from '@/components/no-data/no-data';
	import spEditor from '../public/sp-editor/components/sp-editor/sp-editor.vue';
    export default {
        data() {
            return {
                data_list_loding_status: 1,
				data_list_loding_msg: '',
				params:null,
				company:null,
				config: null,
				user: null,

                // 封面图片
                image_list: [],
                // 路径类型
                editor_path_type: 'common',
				del_serv_url: app.globalData.get_request_url("deleteimg", "info", "classinfo"),
				
                // 内容
                input_length_max: 500,
                input_value: '',

                // 提交按钮
                form_submit_loading: false,
				
				diytheme: app.globalData.get_fcc_theme_color(),
            };
        },
        components: {
			// #ifdef APP-PLUS
			ykAuthpup,
			// #endif
            componentPopup,
            componentUpload,
            componentNoData,
			spEditor
        },

        onLoad(params) {
            // 数据加载
            this.init();
        },

        onShow() {
			app.globalData.page_event_onshow_handle();
		},

        // 下拉刷新
        onPullDownRefresh() {
			this.init();
		},

        methods: {
            init() {
                var user = app.globalData.get_user_info(this, 'init');
                if (user != false) {
					console.log(user);
					this.setData({
					    user: user
					});
                    this.get_data();
                } else {
                    // 提示错误
                    this.setData({
                        data_list_loding_status: 2,
                        data_list_loding_msg: this.$t('form.form.8l3ul5'),
                    });
                }
            },
            get_data() {
                // 加载loding
                uni.showLoading({
                    title: this.$t('common.loading_in_text'),
                });
				
				this.setData({
				    data_list_loding_status: 1,
				});
                uni.request({
                    url: app.globalData.get_request_url('index', 'company', 'classinfo'),
                    method: 'POST',
                    data: {},
                    success: (res) => {
                        uni.hideLoading();
                        if (res.data.code == 0) {
							var info = res.data.data.data || null;
							var base = res.data.data.base || null;
							var image_list = [];
							if((info.wx_ma || null) != null && info.wx_ma != '')
							{
								image_list.push(info.wx_ma);
							}
							this.setData({
							    data_bottom_line_status: true,
							    data_list_loding_status: 3,
							    company: info,
								config: base,
								image_list: image_list,
							});
                        } else {
                            this.setData({
                                data_list_loding_status: 0,
								data_list_loding_msg: res.data.msg,
                            });
                            if (app.globalData.is_login_check(res.data, this, 'get_data')) {
                                app.globalData.showToast(res.data.msg);
                            }
                        }
                    },
                    fail: () => {
                        uni.hideLoading();
                        this.setData({
                            data_list_loding_status: 2,
							data_list_loding_msg: this.$t('common.internet_error_tips')
                        });
                        app.globalData.showToast(this.$t('common.internet_error_tips'));
                    },
                });
            },

            // 上传回调
            retrun_image_event(data) {
                var new_data = this.company;
                new_data.wx_ma = data[0];
				var image_list = [];
				image_list.push(data[0]);
                this.setData({
                    company: new_data,
					image_list: image_list,
                });
            },

            // 回调富文本内容
            rich_text_event(e) {
                var new_data = this.company;
                new_data.content = e.html;
                this.setData({
                    company: new_data,
                });
            },
            initEditor(editor) {
                // 初始化编辑器内容
                editor.setContents({
                    html: (this.company || null) !== null ? this.company.content : '',
                });
            },

            // 上传图片
            up_in_image_event(tempFiles, editorCtx) {
                var self = this;
                // 使用 uniCloud.uploadFile 上传图片的示例方法（可适用多选上传）
                tempFiles.forEach(async (item) => {
                    uni.showLoading({
                        title: self.$t('form.form.2e5rv3'),
                        mask: true,
                    });
                    await uni.uploadFile({
                        url: app.globalData.get_request_url('index', 'ueditor'),
                        // #ifdef APP-PLUS || H5
                        filePath: item.path,
                        // #endif
                        // #ifdef MP-WEIXIN
                        filePath: item.tempFilePath,
                        // #endif
                        name: 'upfile',
                        formData: {
                            action: 'uploadimage',
                            path_type: 'common', // 路径类型，默认common
                        },
                        success: function (res) {
                            let data = JSON.parse(res.data);
                            if (res.statusCode == 200) {
                                // 上传完成后处理
                                editorCtx.insertImage({
                                    src: data.data.url, // 此处需要将图片地址切换成服务器返回的真实图片地址
                                    // width: '50%',
                                    alt: self.$t('form.form.3h58hv'),
                                    success: function (e) {},
                                });
                                uni.hideLoading();
                            }
                        },
                        fail: function (e) {
                            app.globalData.showToast(e.errMsg);
                            uni.hideLoading();
                        },
                    });
                });
            },
			
            // 表单提交
            form_submit(e) {
                // 数据验证
                var validation = [
                    { fields: 'com_describe', msg: '简介不能为空' },
					{ fields: 'contact_tel', msg: '联系电话不能为空' },
                ];
				var validate = {
				    com_describe: e.detail.value.com_describe,
				    contact_tel: e.detail.value.contact_tel,
				};
                if (app.globalData.fields_check(validate, validation)) {
                    uni.showLoading({
                        title: this.$t('buy.buy.r79t77'),
                    });
                    this.setData({
                        form_submit_loading: true,
                    });
					
					var temp_company = this.company;
					temp_company['com_describe'] = e.detail.value.com_describe;
					temp_company['contact_tel'] = e.detail.value.contact_tel;
					temp_company['contact_wx'] = e.detail.value.contact_wx;

                    // 网络请求
                    uni.request({
                        url: app.globalData.get_request_url('editsave', 'company', 'classinfo'),
                        method: 'POST',
                        data: temp_company,
                        dataType: 'json',
                        success: (res) => {
                            uni.hideLoading();
                            if (res.data.code == 0) {
                                app.globalData.showToast(res.data.msg, 'success');
                                setTimeout(function () {
                                    app.globalData.page_back_prev_event();
                                }, 2000);
                            } else {
                                this.setData({
                                    form_submit_loading: false,
                                });
                                if (app.globalData.is_login_check(res.data)) {
                                    app.globalData.showToast(res.data.msg);
                                } else {
                                    app.globalData.showToast(this.$t('common.sub_error_retry_tips'));
                                }
                            }
                        },
                        fail: () => {
                            uni.hideLoading();
                            this.setData({
                                form_submit_loading: false,
                            });
                            app.globalData.showToast(this.$t('common.internet_error_tips'));
                        },
                    });
                }
            }
        },
    };
</script>
<style>
    @import './company-info.css';
</style>
